<?php

namespace platform\controllers;

use common\enum\Enum;
use common\models\Circuit;
use common\models\CircuitAddress;
use common\models\Order;
use common\models\OrderChangeAudit;
use common\models\PointTime;
use common\models\Shift;
use common\models\Site;
use platform\core\BaseController;
use platform\interfaces\ShiftInterface;
use cm;
use platform\models\ShiftModel;
use yii\db\Expression;

class ShiftController extends BaseController implements ShiftInterface
{
    function actionIndex()
    {
        $data = (new ShiftModel())->actionIndex();
        $action = \Yii::$app->request->get('action');
        if($action=='point'){
            return Cm::$res->render($this, "point", $data, '班次管理');
        }
        return Cm::$res->render($this, "index", $data, '班次管理');
    }

    public function actionPoint()
    {
        $params=array_merge(\Yii::$app->request->get(),\Yii::$app->request->post());
        if(empty($params['action'])){
            $id = $params['shift_id'];
            $info=Shift::findOne($id);
            if(!$info)Cm::$res->error('该班次已不存在！');
            $site_id=Circuit::find()->andWhere(['id'=>$info->circuit_id])
                ->select(['site_sid','site_eid'])
                ->column();
            $query=PointTime::find()->alias('pt')
                ->leftJoin(['ca'=>CircuitAddress::tableName()],['pt.ca_id'=>new Expression('ca.id')])
                ->leftJoin(['s'=>Site::tableName()],['s.id'=>new Expression('ca.site_id')])
                ->andWhere(['pt.shift_id'=>$id,'ca.is_del'=>Enum::STATUS_NORMAL])
                ->andWhere(['not in','ca.site_id',$site_id])
                ->select(['pt.*','s.name']);
            if(!empty($params['site_id']))$query->andWhere(['ca.site_id'=>$params['site_id']]);
            $data['count']=(clone $query)->count();
            $data['data']=(clone $query)->asArray()->all();
            foreach ($data['data'] as &$item){
                $item['created_at']=date('Y-m-d H:i:s',$item['created_at']);
                $item['updated_at']=date('Y-m-d H:i:s',$item['updated_at']);
            }
            Cm::$res->paging($data);
        }else{
            if($params['action']){
                $id = $params['id']??0;
                $transaction=\Yii::$app->db->beginTransaction();
                try {
                    if(Cm::$redis->Exists('is_err'))throw new \Exception(Cm::$redis->get('is_err'));
                    $info=PointTime::findOne($id);
                    if(!$info)throw new \Exception('数据已不存在');
                    $info->setAttributes($params);
                    if(!$info->save())throw new \Exception(current($info->getFirstErrors()));
//                    $shift=Shift::findOne($params['shift_id']);
//                    $point_times=array_column($shift->point_times,null,'id');
//                    $point_times[$info->ca_id]['time']=date('H:i',strtotime($params['departuretime']));
//                    $shift->point_times=$point_times;
                    $transaction->commit();
                    Cm::$res->susess([], '保存成功');
                }catch (\Exception $e){
                    $transaction->rollBack();
                    Cm::$res->error('保存失败,'.$e->getMessage());
                }
            }
        }
    }

    public function actionUpdate(){
        foreach (OrderChangeAudit::find()->andWhere(['>=','addtime','2024-06-18 01:35:03'])->each() as $model){
            /** @var $model OrderChangeAudit */
            echo $model->id.PHP_EOL;
            $order=Order::findOne($model->first_order_id);
            $order_model=Order::findOne($model->change_id);
            $order_model->pay_type=$order->pay_type;
            $order_model->pay_time=$order->pay_time;
            $order_model->pay_price=$order->pay_price;
            $order_model->pay_status=$order->pay_status;
            $order_model->rebate_status=$order->rebate_status;
            $order_model->rebate_type=$order->rebate_type;
            $order_model->rebate=$order->rebate;
            $order_model->rebate_price=$order->rebate_price;
            $order_model->rebate_issue=$order->rebate_issue;
            $order_model->bind_uid=$order->bind_uid;
            $order_model->s_id=$order->s_id;
            $order_model->e_id=$order->e_id;
            $order_model->p_id=$order->p_id;
            $order_model->save();
        }
    }
}